package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜品相关")
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dishService.addDish(dishDTO);
        return Result.success();
    }


    /**
     * 分页查询菜品
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult>selectPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("执行菜品的分页查询:{}",dishPageQueryDTO);
        PageResult pageResult=dishService.selectPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 批量删除菜品
     * @return
     */
    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Long>ids){
        log.info("批量删除菜品:{}",ids);
        dishService.deleteBatch(ids);

        return Result.success();
    }


    /**
     * 根据id查询菜品
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> selectById(@PathVariable Long id){
        log.info("根据菜品id{}查询菜品",id);
        DishVO dishVO=dishService.selectById(id);
        return Result.success(dishVO);
    }


    /**
     * 根据分类id查询菜品
     * @return
     */
    @ApiOperation("根据分类查询菜品")
    @GetMapping("/list")
    public Result<List> selectByCategoryId(Long categoryId){
        log.info("根据分类{}查询菜品",categoryId);
        List<Dish>dishList=dishService.selectByCategoryId(categoryId);
        return Result.success(dishList);
    }


    /**
     * 修改菜品
     * @return
     */
    @ApiOperation(value = "修改菜品信息")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息:{}",dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }


    /**
     * 设置菜品起售或停售
     * @return
     */
    @ApiOperation("设置菜品的售卖状态")
    @PostMapping("/status/{status}")
    public Result setStartOrStop(@PathVariable Integer status,Long id){
        log.info("设置商品{}状态为:{}",id,status== StatusConstant.ENABLE?"启售":"停售");
        dishService.setStartOrStop(id,status);
        return Result.success();
    }



}
